var orgId = gUserLoginResult.orgId;
if (gUserLoginResult.orgType == 1 || gUserLoginResult.userActors.indexOf("1") >= 0)
    orgId = -1;
//标牌数量

var actorId;
if (gUserLoginResult.userActors.indexOf("1") >= 0)
    actorId = 1;
else if (gUserLoginResult.userActors.indexOf("7") >= 0)
    actorId = 7;
else
    actorId = 3;

var urlCount = "";
var urlFault = "";
var urlAudit = "";
var urlManage = "";
var urlFaultLoad = "";
var urlAuditLoad = "";
var urlDealTalkTimes = "";
var typeName = "";

$.getCount = function () {
    $.ajax({
        url: urlCount,
        type: 'post',
        data: {
            'orgId': gUserLoginResult.orgId,
            'actorId': actorId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData.length != 0) {
                        $("#signCount").html(msg.jsonData);
                        $("#signCount").click(function () {
                            if (gUserLoginResult.userActors.indexOf("1") < 0
                                && gUserLoginResult.userActors.indexOf("3") < 0
                                && gUserLoginResult.userActors.indexOf("4") < 0) {
                                $.alert("无访问权限", 2);
                            } else
                                $.loadURL('设备管理', typeName + '管理', urlManage);
                        })
                    } else {
                        $.alert("获取数据失败", 2);
                    }
                } else {
                    $.alert("获取数据失败", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
        },
        error: function () {
            $.alert('系统错误，请联系管理员', 2);
        }
    });
};
//标杆数量
$.getRoadPoleCount = function () {
    $.ajax({
        url: '../rds_count/getRoadPoleCount',
        type: 'post',
        data: {
            'orgId': gUserLoginResult.orgId,
            'actorId': actorId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData.length != 0) {
                        $("#poleCount").html(msg.jsonData);
                        $("#poleCount").click(function () {
                            if (gUserLoginResult.userActors.indexOf("1") < 0
                                && gUserLoginResult.userActors.indexOf("3") < 0
                                && gUserLoginResult.userActors.indexOf("4") < 0) {
                                $.alert("无访问权限", 2);
                            } else
                                $.loadURL('设备管理', '标牌管理', 'management/roadSignManage.html');
                        })
                    } else {
                        $.alert("获取数据失败", 2);
                    }
                } else {
                    $.alert("获取数据失败", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
        },
        error: function () {
            $.alert('系统错误，请联系管理员', 2);
        }
    });
};

//故障审核
$.getAuditFault = function () {

    $.ajax({
        url: urlFault,
        type: 'post',
        data: {
            'orgId': gUserLoginResult.orgId,
            'actorId': actorId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData.length != 0) {
                        $("#faultCounts").html(msg.jsonData);
                        $("#faultCounts").click(function () {
                            if (gUserLoginResult.userActors.indexOf("1") >= 0
                                || gUserLoginResult.userActors.indexOf("7") >= 0
                                || gUserLoginResult.userActors.indexOf("3") >= 0)
                                $.loadURL('故障管理', typeName + '故障审核', urlFaultLoad);
                            else
                                $.alert("无访问权限", 2);
                        });
                        $("#editFault").click(function () {
                            if (gUserLoginResult.userActors.indexOf("1") >= 0
                                || gUserLoginResult.userActors.indexOf("7") >= 0
                                || gUserLoginResult.userActors.indexOf("3") >= 0)
                                $.loadURL('故障管理', typeName + '故障审核', urlFaultLoad);
                            else
                                $.alert("无访问权限", 2);
                        });
                    } else {
                        $.alert("获取数据失败", 2);
                    }
                } else {
                    $.alert("获取数据失败", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
        },
        error: function () {
            $.alert('系统错误，请联系管理员', 2);
        }
    });
};
//标牌审核
$.getAuditSign = function () {
    $.ajax({
        url: urlAudit,
        type: 'post',
        data: {
            'orgId': gUserLoginResult.orgId,
            'actorId': actorId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData.length != 0) {
                        $("#auditCounts").html(msg.jsonData);
                        $("#auditCounts").click(function () {
                            if (gUserLoginResult.userActors.indexOf("1") >= 0
                                || gUserLoginResult.userActors.indexOf("3") >= 0)
                                $.loadURL('设备管理', typeName + '审核', urlAuditLoad);
                            else
                                $.alert("无访问权限", 2);
                        });
                        $("#editSign").click(function () {
                            if (gUserLoginResult.userActors.indexOf("1") >= 0
                                || gUserLoginResult.userActors.indexOf("3") >= 0)
                                $.loadURL('设备管理', typeName + '审核', urlAuditLoad);
                            else
                                $.alert("无访问权限", 2);
                        });
                    } else {
                        $.alert("获取数据失败", 2);
                    }
                } else {
                    $.alert("获取数据失败", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
        },
        error: function () {
            $.alert('系统错误，请联系管理员', 2);
        }
    });
};

//故障统计柱状图
$.getFalutCountMessage = function (orgId) {
    var year = new Date().getFullYear();
    $("#head").html(year + "年年度故障统计信息");
    $.ajax({
        url: urlDealTalkTimes,
        type: 'post',
        data: {
            year: year,
            orgId: orgId,
            userId: gUserLoginResult.userId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                var timeJson = msg.jsonData;
                arryTalk = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
                for (var i = 0; i < timeJson.length; i++) {
                    var time = timeJson[i].time;
                    var timearr = time.replace(" ", ":").replace(/\:/g, "-").split("-");
                    var month = timearr[1].substring(1);
                    if (timeJson[i].type == 0) {
                        arryTalk[month - 1]++;
                    }
                }
                var areaChartData = {
                    labels: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                    datasets: [
                        {
                            label: "Electronics",
                            fillColor: "rgba(210, 214, 222, 1)",
                            strokeColor: "rgba(210, 214, 222, 1)",
                            pointColor: "rgba(210, 214, 222, 1)",
                            pointStrokeColor: "#c1c7d1",
                            pointHighlightFill: "#fff",
                            pointHighlightStroke: "rgba(220,220,220,1)",
                            data: arryTalk
                        }
                    ]
                };
                var barChartCanvas = $("#barChart").get(0).getContext("2d");
                var barChart = new Chart(barChartCanvas);
                var barChartData = areaChartData;
                barChartData.datasets[0].fillColor = "#3D8EBC";
                barChartData.datasets[0].strokeColor = "#3D8EBC";
                barChartData.datasets[0].pointColor = "#3D8EBC";
                var barChartOptions = {
                    responsive: true
                };
                barChartOptions.datasetFill = false;
                barChart.Bar(barChartData, barChartOptions);
            }
        }
    })
};
//首页公告
$.getNoticeListHome = function (userId) {
    $.ajax({
        url: '../rds_noticelist/getNoticeListHome',
        type: 'post',
        data: {'userId': userId},
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData.length != 0) {
                        $("#notice").empty();
                        for (var i = 0; i < msg.jsonData.length; i++) {
                            var content = msg.jsonData[i].noticeContent.replace(/<.*?>/ig, "").toString().trim();
                            if (content.length > 20) {
                                content = content.substring(0, 20) + "...";
                            }

                            if (i < 3) {
                                if (msg.jsonData[i].isRead == 0) {
                                    $("#notice").append("<li class=\"item\">" +
                                        "<a href=\"javascript:void(0)\" class=\"product-title\" onclick=\"$.shownoticeModal(" + msg.jsonData[i].noticeId + "," + msg.jsonData[i].userNoticeId + ")\">" + msg.jsonData[i].noticeTitle + "</a>" +
                                        "<span class=\"label label-danger pull-right\">new</span>" +
                                        "<div><span class=\"product-description\" style='display:inline-block'>" + content + "</span><span class='product-description' style='float: right'>" + msg.jsonData[i].noticeSendTime.substring(0, 10) + " </span></div></li>");
                                } else {
                                    $("#notice").append("<li class=\"item\">" +
                                        "<a href=\"javascript:void(0)\" class=\"product-title\" onclick=\"$.shownoticeModal(" + msg.jsonData[i].noticeId + "," + msg.jsonData[i].userNoticeId + ")\">" + msg.jsonData[i].noticeTitle + "</a>" +
                                        "<div><span class=\"product-description\">" + content + "</span><span class='product-description' style='float: right'>" + msg.jsonData[i].noticeSendTime.substring(0, 10) + " </span></div></li>");
                                }
                            }


                        }

                    } else {
                        $("#notice").empty();
                        $("#notice").append("<li class=\"item\">" +
                            "<span class=\"product-title\">暂无公告</span>" +
                            "<span class=\"product-description\">暂无公告 &nbsp&nbsp&nbsp </span></li>");
                    }
                } else {
                    $.alert("获取数据失败", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
        },
        error: function (msg) {
            $.alert('系统错误，请联系管理员', 2);
        }
    });
};


$(function () {
    if (gUserLoginResult.userActors.indexOf("1") >= 0)
        $("#feedBack").hide();
    $.getNoticeListHome(gUserLoginResult.userId);
    switch (sessionStorage.outType) {
        case '0':
            $.getRoadPoleCount();
            $("#roadPoleDiv").show();
            typeName = "标牌";
            urlManage = "management/roadSignManage.html";
            urlCount = "../rds_count/getRoadSignCount";
            urlAudit = "../rds_roadSignAudit/getRoadSignAuditCount";
            urlFault = "../rds_count/getSignFaultCount";
            urlDealTalkTimes = "../rds_count/getDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/currentFault.html";
            else
                urlFaultLoad = "fault/faultreport.html";
            urlAuditLoad = "management/roadSignAudit.html";
            break;
        case '2':
            typeName = "信号灯";
            urlManage = "management/signalLampManage.html";
            urlCount = "../rds_count/getSignalLampCount";
            urlAudit = "../rds_count/getSignalLampAuditCount";
            urlFault = "../rds_count/getSignalLampFaultCount";
            urlDealTalkTimes = "../rds_count/getSignLampDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/signalLampCheck.html";
            else
                urlFaultLoad = "fault/signalLampReport.html";
            urlAuditLoad = "management/signalLampAudit.html";
            break;
        case '3':
            typeName = "信号机";
            urlManage = "management/signalMachineManage.html";
            urlCount = "../rds_count/getSignalMachineCount";
            urlAudit = "../rds_count/getSignalMachineAuditCount";
            urlFault = "../rds_count/getSignalMachineFaultCount";
            urlDealTalkTimes = "../rds_count/getSignalMachineDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/signalMachineCheck.html";
            else
                urlFaultLoad = "fault/signalMachineReport.html";
            urlAuditLoad = "management/signalMachineAudit.html";
            break;
        case '4':
            typeName = "太阳能";
            urlManage = "management/solarEnergyManage.html";
            urlCount = "../rds_count/getSolarEnergyCount";
            urlAudit = "../rds_count/getSolarEnergyAuditCount";
            urlFault = "../rds_count/getSolarEnergyFaultCount";
            urlDealTalkTimes = "../rds_count/getSolarEnergyDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/solarEnergyCheck.html";
            else
                urlFaultLoad = "fault/solarEnergyReport.html";
            urlAuditLoad = "management/solarEnergyAudit.html";
            break;
        case '5':
            typeName = "限高杆";
            urlManage = "management/highLeverManage.html";
            urlCount = "../rds_count/getHighLeverCount";
            urlAudit = "../rds_count/getHighLeverAuditCount";
            urlFault = "../rds_count/getHighLeverFaultCount";
            urlDealTalkTimes = "../rds_count/getHighLeverDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/highLeverCheck.html";
            else
                urlFaultLoad = "fault/highLeverReport.html";
            urlAuditLoad = "management/highLeverAudit.html";
            break;
        case '6':
            typeName = "标线";
            urlManage = "management/roadLineManage.html";
            urlCount = "../rds_count/getRoadLineCount";
            urlAudit = "../rds_count/getRoadLineAuditCount";
            urlFault = "../rds_count/getRoadLineFaultCount";
            urlDealTalkTimes = "../rds_count/getRoadLineDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/roadLineCheck.html";
            else
                urlFaultLoad = "fault/roadLineReport.html";
            urlAuditLoad = "management/roadLineAudit.html";
            break;
        case '7':
            typeName = "护栏";
            urlManage = "management/guardrailManage.html";
            urlCount = "../rds_count/getGuardrailCount";
            urlAudit = "../rds_count/getGuardrailAuditCount";
            urlFault = "../rds_count/getGuardrailFaultCount";
            urlDealTalkTimes = "../rds_count/getGuardrailDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/guardrailCheck.html";
            else
                urlFaultLoad = "fault/guardrailReport.html";
            urlAuditLoad = "management/guardrailAudit.html";
            break;
        case '8':
            typeName = "监控箱";
            urlManage = "management/monitorManage.html";
            urlCount = "../rds_count/getMonitorCount";
            urlAudit = "../rds_count/getMonitorAuditCount";
            urlFault = "../rds_count/getMonitorFaultCount";
            urlDealTalkTimes = "../rds_count/getMonitorDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/monitorCheck.html";
            else
                urlFaultLoad = "fault/monitorReport.html";
            urlAuditLoad = "management/monitorAudit.html";
            break;
        case '9':
            typeName = "窨井";
            urlManage = "management/coverManage.html";
            urlCount = "../rds_count/getCoverCount";
            urlAudit = "../rds_count/getCoverAuditCount";
            urlFault = "../rds_count/getCoverFaultCount";
            urlDealTalkTimes = "../rds_count/getCoverDealTalkTimes";
            if (gUserLoginResult.userActors.indexOf("7") >= 0)
                urlFaultLoad = "fault/coverCheck.html";
            else
                urlFaultLoad = "fault/coverReport.html";
            urlAuditLoad = "management/coverAudit.html";
            break;
    }
    $("#countType").text(typeName + "数量");
    $.getCount();
    if (gUserLoginResult.userActors.indexOf("1") >= 0
        || gUserLoginResult.userActors.indexOf("7") >= 0
        || gUserLoginResult.userActors.indexOf("3") >= 0) {
        if (gUserLoginResult.userActors.indexOf("7") >= 0) {
            $("#type0").hide();
            $.getAuditFault();
        } else {
            $.getAuditFault();
            $.getAuditSign();
        }
    } else {
        $("#type0").hide();
        $("#type1").hide();
    }

    $.getFalutCountMessage(orgId);
    $("#showMore").click(function () {
        $.loadURL('公告管理', '公告列表', 'notice/noticeList.html');
    });
});


$.shownoticeModal = function (noticeId, userNoticeId) {
    $.ajax({
        url: "../rds_noticelist/getNoticeByNotceId",
        type: "post",
        dataType: "json",
        data: {
            noticeId: noticeId
        },
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                $("#noticeTitle").text(msg.jsonData.noticeTitle);
                $("#noticeTime").text(msg.jsonData.noticeSendTime.substring(0, 10));
                $("#userName").text(msg.jsonData.createUserName);
                $("#noticeContent").html(msg.jsonData.noticeContent);
            }
        },
        error: function (msg) {
            $.alert("系统错误！", 4);
        }
    });
    $.ajax({
        url: "../rds_noticelist/updateIsRead",
        type: "post",
        dataType: "json",
        data: {
            userNoticeId: userNoticeId
        },
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                $.getNoticeListHome(gUserLoginResult.userId);
            }
        },
        error: function (msg) {
            $.alert("系统错误！", 4);
        }
    });

    $("#showInfoModal").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });

};

// 关闭showInfoModal
$.closeShowModal = function () {
    $("#showReason").val("");
    $("#showInfoModal").modal("hide");
    $("#showNewsInfoModal").modal("hide");
};


var customTemplates = {
    image: function (context) {
        return "<li>" +
            "<a class='btn btn-default' data-wysihtml5-command='insertImage' title='" + context.locale.image.insert +
            "' tabindex='-1' onclick='$.uploadPhoto()'><span class='glyphicon glyphicon-picture'></span></a>" +
            "</li>";
    }
};

$("#feedBackContent").wysihtml5({
    customTemplates: customTemplates
});

$("#feedBack").click(function () {
    $("#modal-feedBack").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
});

$.addFeedBack = function () {
    if ($("#feedBackContent").val() == 0)
        $.alert("请填写内容", 2);
    else {
        bootbox.confirm({
            buttons: {
                confirm: {
                    label: '<i class="fa fa-check"></i> 确定',
                    className: 'btn btn-primary btn-flat'
                },
                cancel: {
                    label: '<i class="fa fa-close"></i> 取消',
                    className: 'btn btn-warning btn-flat pull-left'
                }
            },
            message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定提交该反馈吗?</b>',
            callback: function (result) {
                if (result) {
                    $.ajax({
                        url: "../rds_feedback/addFeedBack",
                        type: "post",
                        dataType: "json",
                        data: {
                            rapporteurId: gUserLoginResult.userId,
                            feedBackCreateTime: $.getTime(),
                            feedBackContent: $("#feedBackContent").val()
                        },
                        success: function () {
                            $.alert("发送成功！", 1);
                            $.closeNotice();
                        },
                        error: function (msg) {
                            $.alert("系统错误！", 4);
                        }
                    });
                }
            }
        });
    }

};

$.closeNotice = function () {
    $('#feedBackContent').parent().find('.wysihtml5-toolbar').remove();
    $('#feedBackContent').parent().find('iframe').remove();
    $('#feedBackContent').parent().find('input[name*="wysihtml5"]').remove();
    $('#feedBackContent').show();
    $('#feedBackContent').val("");
    $("#modal-feedBack").modal("hide");
    $("#feedBackContent").wysihtml5();
};

// 打开上传modal框
$.uploadPhoto = function () {
    $("#upPictureModal").modal({
        backdrop: 'static',
        keyboard: true,
        show: true
    });
};

// 关闭上传modal框
$.closeUpPtModal = function () {
    $("#upPictureModal").modal("hide");
    $("#ossfile").empty();
    clearOssData();
};

//获取图片信息
$.doneUpPicture = function () {
    var fileInfo = getOssFileInfo();
    if (fileInfo.length == 0) {
        $.alert("未上传图片", 2);
    } else if (fileInfo.length > 1) {
        $.alert("只需上传一张图片作为证明材料", 3);
    } else {
        $.addImage(fileInfo[0].fileUrl);
        $.closeUpPtModal();
    }
};

$.addImage = function (newUrl) {
    var str = $('#feedBackContent').val();
    $('#feedBackContent').parent().find('.wysihtml5-toolbar').remove();
    $('#feedBackContent').parent().find('iframe').remove();
    $('#feedBackContent').parent().find('input[name*="wysihtml5"]').remove();
    $('#feedBackContent').show();
    if (str.indexOf("<img>") != 0)
        str = str.substring(0, str.indexOf("<img>") + 4) +
            "  width=300px src = '" + newUrl + "'/ " +
            str.substring(str.indexOf("<img>") + 4);
    $('#feedBackContent').val(str);
    $("#feedBackContent").wysihtml5();
};


//判断文件类型(在需要的js文件添加，文件名务必保持一致)
function judgeFileType(suffix) {
    //图片文件
    if (!(suffix == ".jpg" || suffix == ".JPG" || suffix == ".png" || suffix == ".PNG" ||
            suffix == ".jpeg" || suffix == ".JPEG" || suffix == ".gif" || suffix == ".GIF" ||
            suffix == ".bmp" || suffix == ".BMP")) {
        alert("请选择图片文件！");
        return false;
    }
}

$.getTime = function () {
    $.changeTimeType = function (date) {
        if (date >= 0 && date <= 9) {
            date = "0" + date;
        }
        return date;
    };
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = $.changeTimeType(date.getMonth() + 1);
    var strDate = $.changeTimeType(date.getDate());
    var hour = $.changeTimeType(date.getHours());
    var minutes = $.changeTimeType(date.getMinutes());
    var seconds = $.changeTimeType(date.getSeconds());

    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + hour + seperator2 + minutes
        + seperator2 + seconds;
    return currentdate;
};